<?php
namespace app\access\controller;

use tp5_lib\common\controller\TestBaseController;
use tp5_lib\access\logic\PrivilegeLogic;

/**
 * 测试PrivilegeLogic
 * 不关联用户和角色
 */
class TestController extends TestBaseController
{
    protected function getPrivilege()
    {
        return PrivilegeLogic::instance();
    }
    public function index()
    {
        $privilege=$this->getPrivilege();
        $list=$privilege->getPrivileges();
        return $this->fetch('',[
            "priv_list" => $list,
            "priv_str" => $privilege->stringify($list),
        ]);
    }
    /**
     * 授权
     */
    public function accredit()
    {
        $_privilege=input("privilege","");
        $privilege=$this->getPrivilege();
        $privilege->accredit($_privilege);
        $this->success("授权成功");
    }
    public function check()
    {
        $privilege=$this->getPrivilege();
        $all=$privilege->getPrivileges();
        echo $privilege->stringify($all) ."\r\n";
        
        $list=[
            "test",
            "test.main",
            "test.other",
            "test.main.index",
            "demo",
            "demo.main",
            "demo.other",
            "test.other&demo.main",
            "test.other|demo.main",
        ];
        $result=[];
        foreach($list as $item){
            $result[$item] = $privilege->check($item) ? "true" : "false";
        }
        $this->result($result);
    }
    public function clear()
    {
        $this->getPrivilege()->clear();
        $this->success("清理成功");
    }
}
